/* 
 * Copyright (C) 2012 Martin Řehánek <rehan at mzk.cz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

goog.provide('kramerius.audio.AudioPlayer');

/**
 * @constructor 
 * @param {kramerius.audio.Controller} controller
 */
kramerius.audio.AudioPlayer = function(controller){
    this.controller_ = controller;
    console.log("AudioPlayer: initializing");
    /** @private */
    this.nowPlaying_ = undefined;
    soundManager.setup(
    {   // where to find flash SWFs, as needed
        url: 'soundmanager/swf'
    }
    );
};


/**
 * Sets track to be played. Already playing track is released.
 * @param {kramerius.audio.Track} track
 */
kramerius.audio.AudioPlayer.prototype.setTrack = function(track){
    var audioPlayer = this;
    soundManager.onready(function() {
        //TODO: rict kontrolorovi, ze hraju, at aktualizuje playlist
        var nowPlaying = audioPlayer.nowPlaying_;
//        if (audioPlayer.nowPlaying_ != undefined){
//            audioPlayer.nowPlaying.destruct();
//        }
        if (nowPlaying != undefined){
            nowPlaying.destruct();
        }

        console.log("track to be played: " + track);
        audioPlayer.nowPlaying_ = soundManager.createSound({
            id: track.getId(),
            url: track.urlArray(),
            autoLoad: true,
            //autoPlay: true,
            volume: 50,
            multiShotEvents: true,
            onfinish: function(){
            //TODO: rekni controllerovi, at spusti dalsi vec z playlistu
                controller.playNextTrackFromPlaylist(); 
            },
            whileloading: function(){
                controller.setTotalLength(this.durationEstimate);
            },
            whileplaying: function(){
                controller.setPosition(this.position);
            },
            onplay: function(){
                controller.setPlayerState('playing');
            },
            onpause: function(){
                controller.setPlayerState('paused');
            },
            onresume: function(){
                controller.setPlayerState('playing');
            },
            onstop: function(){
                controller.setPlayerState('stopped');
            },
            ondataerror: function(){
                console.log("AudioPlayer: ERROR: error reading audio data");
                controller.setPlayerState('empty');
            },
            onid3: function(){
                //TODO: ma to smysl? budou mit alespon MP3 id3?
                console.log("AudioPlayer: recieved id3 data ");
            }
        });
    });
    this.controller_.setPlayerState('stopped');
};


kramerius.audio.AudioPlayer.prototype.play = function(){
    console.log("AudioPlayer: playing " + this.nowPlaying_.id);
    this.nowPlaying_.play();
}


kramerius.audio.AudioPlayer.prototype.pause = function(){
    console.log("AudioPlayer: pausing " + this.nowPlaying_.id);
    this.nowPlaying_.pause();
}


kramerius.audio.AudioPlayer.prototype.stop = function(){
    console.log("AudioPlayer: stopping " + this.nowPlaying_.id);
    this.nowPlaying_.stop();
}


kramerius.audio.AudioPlayer.prototype.resume = function(){
    console.log("AudioPlayer: resuming " + this.nowPlaying_.id);
    this.nowPlaying_.resume();
}


kramerius.audio.AudioPlayer.prototype.isPaused = function(){
    return (this.nowPlaying_ != undefined && this.nowPlaying_.paused);
}


kramerius.audio.AudioPlayer.prototype.isPlayingOrBuffering = function(){
    return (this.nowPlaying_ != undefined 
        && this.nowPlaying_.playState == 1);
}


kramerius.audio.AudioPlayer.prototype.isStoppedOrUninitialized = function(){
    return (this.nowPlaying_ != undefined 
        && this.nowPlaying_.playState == 0);
}


kramerius.audio.AudioPlayer.prototype.setVolume = function(volumeLevel){
    this.nowPlaying_.setVolume(volumeLevel);
}


kramerius.audio.AudioPlayer.prototype.setPosition = function(position){
    if (this.nowPlaying_ != undefined
        && kramerius.audio.utils.differsByAtLeast(this.nowPlaying_.position, position, 100)){
        this.nowPlaying_.setPosition(position);
    //console.log("AudioPlayer: updating position");
    }
}







